<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\facade\Request;
use app\common\model\CashDraws as CashDrawsModel;

//提现记录
class CashDraws extends Controller
{

    protected $middleware = [
        'app\http\middleware\checkAdminToken::class'
    ];

    //  提现的申请列表
    public function index()
    {
        //
        $cash_status=Request::param('cash_status',0);   //-1:提现失败 0:待处理 1:提现成功
        $target_type=Request::param('target_type',1);   //0:用户 1:商家 2.骑手
        $key=Request::param('key',null);
        $value=Request::param('value',null);

        $page=Request::param('page',1);
        $pageSize=Request::param('pageSize',20);
        $where=[
            "cash_status"=>$cash_status,
            "target_type"=>$target_type
        ];

        //关键字搜索
        if($key!=null && $value!=null){
            $where[$key]=$value;
        }

        $count=CashDrawsModel::where($where)->count();
        $page_num=ceil($count/$pageSize);

        $list=  CashDrawsModel::where($where)
            ->page($page,$pageSize)
            ->select();

        $data=[
            'list'=>$list,
            'page_num'=>$page_num,
            'page_current'=>$page
        ];

        return toResful(200,$data,'');
    }

    //同意提现申请
    public function agree()
    {

        $target_type=Request::param('target_type',1);   //0:用户 1:商家 2.骑手
        $cash_id=Request::param('cash_id',0);

        $cash=CashDrawsModel::where([
            'id'=>$cash_id,
            'cash_status'=>0,           //-1:提现失败 0:待处理 1:提现成功
            'target_type'=>$target_type
        ])->find();

        if($cash!=null){
            $cash->cash_status=1;
           $res= $cash->save();
        }else{
            $this->result('',400,'数据错误');
            return;
        }

        if($res==1){
            $this->result('',200,'提现成功');
            return;
        }else{
            $this->result('',400,'提现失败');
            return;
        }
    }

    //拒绝提现申请
    public function disagree()
    {
        $target_type=Request::param('target_type',1);   //0:用户 1:商家 2.骑手
        $cash_id=Request::param('cash_id',0);

        $cash=CashDrawsModel::where([
            'id'=>$cash_id,
            'cash_status'=>0,           //-1:提现失败 0:待处理 1:提现成功
            'target_type'=>$target_type
        ])->find();

        if($cash!=null){
            $cash->cash_status=-1;
            $res= $cash->save();
        }else{
            $this->result('',400,'数据错误');
            return;
        }

        if($res==1){
            $this->result('',200,'提现成功');
            return;
        }else{
            $this->result('',400,'提现失败');
            return;
        }
    }


    public function create()
    {
        //
    }


}
